<?php
namespace apptest\index\controller;

class GetLink
{
    public function index()
    {
        /*
         * 获取网页中所有的a标签连接
         * */
        //$file = file_get_contents('./html.txt');
        $handle = fopen('./html.txt', "r");
        while(!feof($handle))
        {
            $file = fgets($handle);
            $file = html_entity_decode($file);
            //file_put_contents('./new.txt', $file, FILE_APPEND);
            //$file = "\n" . $file . "\n";
            //file_put_contents('./link.txt',$file,FILE_APPEND);
            //p($file);
            $htm = str_replace("\n", '', $file);
            $htm = preg_replace('/<a/i', "\n<a", $htm);
            $htm = preg_replace('/<\/a>/i', "</a>\n", $htm);
            preg_match_all('/<a\s*.*>.*?<\/a>/', $htm, $matched);
            p($matched);
            foreach ($matched[0] as $key => $value)
            {
                //echo '链接：' . $value . "<br>";
                //$txt = "\n" . $value . "\n";
                file_put_contents('./link.txt',$value,FILE_APPEND);
            }
        }
        fclose($handle);
    }
}

